package com.powernode.springmvc.controller;

import com.powernode.springmvc.bean.User;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.RequestEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.net.URI;

@Controller
public class RequestBodyController {
    @RequestMapping("/testResponseBody")
    @ResponseBody
    public String testResponseBody(){
        //当处理器方法上添加了@ResponseBody，那么这个方法的返回值不再是逻辑视图名称
        //而是直接将字符串响应给浏览器了
        //也可以返回json字符串
        //return "{\"id\":\"1\",\"name\":\"jack\"}";
        return "hello ajax";
    }

    @RequestMapping("/testReturnJson")
    @ResponseBody
    public User testReturnJson(){
        User user = new User(111L,"lisi","123",22);
        return user;  //使用@ResponseBody注解，并且引入jackson依赖，
        // 可以将user转成json字符串，并响应给浏览器
    }


    @RequestMapping(value = "/testPage",method = RequestMethod.POST)
    public String testPage(){
        return "test";
        //跳转页面也是同一个请求，你跳转过去了要进行数据的提交，所以要用post请求

    }

//    @RequestMapping(value = "/testPost")
//    @ResponseBody
//    public String testPost(@RequestParam("username")
//                             String username,
//                         @RequestParam("password")
//                         String password){
//        return username+":"+password;
//
//    }

//    @RequestMapping(value = "/testPost")
//    @ResponseBody
//    //post请求才有请求体
//    public String testRequestBody(@RequestBody String requestBodyStr){
//        return requestBodyStr;
//    }

    @RequestMapping(value = "/testPost")
    @ResponseBody
    //post请求才有请求体
    public User testRequestBody(@RequestBody User user){
        System.out.println(user.getAge());
        return user;
    }

    @RequestMapping("/testRequestEntity")
    @ResponseBody
    public String testRequestEntity(RequestEntity<User> requestEntity){
        //获取请求方法
        HttpMethod method = requestEntity.getMethod();
        System.out.println(method);//POST
        //获取请求url
        URI url = requestEntity.getUrl();
        System.out.println(url);
        //http://192.168.108.1:8080/springmvc07/testRequestEntity
        //获取请求头
        HttpHeaders headers = requestEntity.getHeaders();
        System.out.println(headers);
        //[host:"192.168.108.1:8080", connection:"keep-alive", content-length:"47", user-agent:"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36", content-type:"application/json;charset=UTF-8", accept:"*/*", origin:"http://192.168.108.1:8080", referer:"http://192.168.108.1:8080/springmvc07/testPage", accept-encoding:"gzip, deflate", accept-language:"zh-CN,zh;q=0.9", cookie:"id=123456789"]
        //获取请求体
        User body = requestEntity.getBody();
        System.out.println(body);
        //User{username='king', password='121', age=19}
        return "study RequestEntity!";
    }


}
